home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 501 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.4 KB

  1. Path: surfnet.nl!sun4nl!ittpub!ittpub!nntp
  2. Newsgroups: comp.std.c
  3. Subject: Array vs. structure alignment
  4. Message-ID: <1996Mar5.114346.1790@ittpub>
  5. From: wil@ittpub.nl (Wil Evers)
  6. Date: 5 Mar 96 11:43:46 WET
  7. Distribution: world
  8. Nntp-Posting-Host: lintilla
  9.  
  10. A few days ago, I was working with some legacy header files having lots of  
  11. typedefs like this:
  12.  
  13. typedef char NAME_TYPE        [NAME_SIZE];
  14. typedef char ADDRESS_TYPE    [ADDRESS_SIZE];
  15. typedef char CITY_TYPE        [CITY_SIZE];
  16.  
  17. These typedefs are a nuisance, because they hide their array nature,  
  18. obscuring code where these types are passed between functions. I tried to  
  19. fix this by adding:
  20.  
  21. typedef struct { char data[NAME_SIZE]; }     PROPER_NAME_TYPE;
  22. typedef struct { char data[ADDRESS_SIZE]; }     PROPER_ADDRESS_TYPE;
  23. typedef struct { char data[CITY_SIZE]; }     PROPER_CITY_TYPE;
  24.  
  25. So I could write new code in terms of the PROPER_* types. This bit me when  
  26. I tried to add a proper type for a composite structure. The old code has:
  27.  
  28. typedef struct {
  29.     NAME_TYPE    name;
  30.     ADDRESS_TYPE    address;
  31.     CITY_TYPE    city;
  32. } CUSTOMER_TYPE;
  33.  
  34. so I added:
  35.  
  36. typedef struct {
  37.     PROPER_NAME_TYPE    name;
  38.     PROPER_ADDRESS_TYPE    address;
  39.     PROPER_CITY_TYPE    city;
  40. } PROPER_CUSTOMER_TYPE;
  41.  
  42. To my surprise, on one of the platforms I tested this (NextStep running on  
  43. an m68k) the offsets of both the `address' and `city' fields in  
  44. CUSTOMER_TYPE and PROPER_CUSTOMER_TYPE were different. This means I cannot  
  45. safely cast a pointer to a CUSTOMER_TYPE to a pointer to a  
  46. PROPER_CUSTOMER_TYPE or vice-versa. Being able to do these casts is  
  47. critical to me, because I need to interface to some of the legacy code  
  48. from my new code.
  49.  
  50. Regarding this, I have two questions:
  51.  
  52. 1. Is there any technical reason why the memory layouts of CUSTOMER_TYPE  
  53. and PROPER_CUSTOMER_TYPE would be different? It seems to me that wrapping  
  54. an array into a structure is something relevant at compile-time, not at  
  55. run time. IMHO the generated code for accessing the fields in a  
  56. CUSTOMER_TYPE could be exactly the same as the code for accessing the  
  57. fields in a PROPER_CUSTOMER_TYPE.
  58.  
  59. 2. Assuming there is no technical reason for this difference, shouldn't  
  60. the C standard require that the layouts of these two types be the same?  
  61. [Please don't ask me for an exact phrasing - I'll happily leave that to  
  62. the experts.]
  63.  
  64. Any comments? Suggestions?
  65.  
  66. - Wil
  67.  
  68. Wil Evers, <wil@ittpub.nl>, ITT Publitec Research and Development BV,
  69. Amsterdam, Holland
  70.